package byteBuddy;

import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.implementation.bytecode.assign.Assigner;

import java.lang.reflect.Method;

/**
 *
 */
@Slf4j
public class MockMethodAdvice {


    @Advice.OnMethodEnter
            (skipOn = Advice.OnNonDefaultValue.class)
    public static Object enter(
            @Advice.Origin Class clazz,
            @Advice.This(optional = true, typing = Assigner.Typing.DYNAMIC) Object thisObject,
            @Advice.Origin Method method,
            @Advice.AllArguments(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object[] allArguments,
            @Advice.Origin("#m") String methodName,
            @Advice.Origin("#s") String signature,
            @Advice.Origin("#r") String returnType) {

        try {
            System.out.println("begin mock");
        } catch (Throwable tx) {
            tx.printStackTrace();
        }
        return null;
    }

    @Advice.OnMethodExit
    public static void exit(
            @Advice.This(optional = true, typing = Assigner.Typing.DYNAMIC) Object thisObject,
            @Advice.Origin Class clazz,
            @Advice.Origin Method method,
            @Advice.AllArguments(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object[] allArguments,
            @Advice.Origin("#s") String signature,
            @Advice.Origin("#r") String returnType,
            @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object returnValue) {
        try {
            System.out.println("end mock");
        } catch (Throwable tx) {
            tx.printStackTrace();
        }
    }
}
